package common

import (
	"git.mycaigou.com/gfyx/common/simplecopier"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	common2 "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/common"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/common"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
	"strconv"
)

type CommonController struct {
	controller.BaseController
	commonService common.CommonServiceIface
}

// NewCommonController
func NewCommonController(
	commonService common.CommonServiceIface,
) *CommonController {
	return &CommonController{
		commonService: commonService,
	}
}

// RegisterRouter
// 注册路由
func (c *CommonController) RegisterRouter(g *gin.RouterGroup) {
	// 获取省市地址
	g.GET("/get-location", c.GetLocation)
	g.GET("/fuzzy-search-company-info", c.FuzzySearchCompanyInfo)

	g.GET("/list-company-type-options", c.ListCompanyTypeOptions)
	// 获取公司名下的项目信息列表
	g.GET("/get-project-list-by-developer-name", c.GetProjectListByCompanyName)
}

// GetLocation 获取省市地址
func (c *CommonController) GetLocation(ctx *gin.Context) {
	data, err := c.commonService.GetLocation(ctx.Request.Context())
	if err != nil {
		stark.Logger.Errorf(ctx.Request.Context(), "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	mData, _ := simplecopier.SliceStructToSliceMapByJsonTag(data)
	c.Success(ctx, mData)
}

// FuzzySearchCompanyInfo 模糊搜索企业工商信息（调用openapi接口）
func (c *CommonController) FuzzySearchCompanyInfo(ctx *gin.Context) {
	keyword := ctx.Query("keyword")
	if keyword == "" {
		c.Success(ctx, make([]map[string]interface{}, 0))
		return
	}

	data, err := c.commonService.FuzzySearchCompanyInfo(ctx.Request.Context(), keyword)
	if err != nil {
		stark.Logger.Errorf(ctx.Request.Context(), "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, data)
}

func (c *CommonController) ListCompanyTypeOptions(ctx *gin.Context) {

	result := make([]*common2.CompanyTypeOption, 0)
	result = append(result, &common2.CompanyTypeOption{Label: "央企控股", Value: "央企控股"})
	result = append(result, &common2.CompanyTypeOption{Label: "国有控股", Value: "国有控股"})
	result = append(result, &common2.CompanyTypeOption{Label: "民企", Value: "民企"})
	result = append(result, &common2.CompanyTypeOption{Label: "集体所有制", Value: "集体所有制"})
	result = append(result, &common2.CompanyTypeOption{Label: "个体户", Value: "个体户"})
	result = append(result, &common2.CompanyTypeOption{Label: "合资", Value: "合资"})
	result = append(result, &common2.CompanyTypeOption{Label: "外资", Value: "外资"})
	result = append(result, &common2.CompanyTypeOption{Label: "事业单位", Value: "事业单位"})
	result = append(result, &common2.CompanyTypeOption{Label: "港澳台", Value: "港澳台"})
	result = append(result, &common2.CompanyTypeOption{Label: "其他", Value: "其他"})

	c.Success(ctx, result)
}

// GetProjectListByCompanyName 获取公司名下的项目信息列表
// 如果公司是甲方，则返回拿地、在建、开盘信息
// 如果公司是乙方，则返回合作案例信息
func (c *CommonController) GetProjectListByCompanyName(ctx *gin.Context) {
	var (
		errMsg string
		req    = common2.GetProjectListByCompanyNameRequest{}
	)
	// company_type  1:开发商 2:供应商
	req.CompanyType, _ = strconv.Atoi(ctx.Query("company_type"))
	req.CompanyName = ctx.Query("company_name")
	req.ProjectName = ctx.Query("project_name")
	req.Page, _ = strconv.Atoi(ctx.DefaultQuery("page", "1"))
	req.PageSize, _ = strconv.Atoi(ctx.DefaultQuery("page_size", "10"))
	if req.CompanyType != 1 && req.CompanyType != 2 {
		errMsg = "company_type 不合法"
	} else if req.CompanyName == "" {
		errMsg = "company_name 不能为空"
	}
	if errMsg != "" {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, errMsg)
		return
	}

	data, err := c.commonService.GetProjectListByCompanyName(ctx.Request.Context(), &req)
	if err != nil {
		stark.Logger.Errorf(ctx.Request.Context(), "错误code:", errcode.COMMON_DATA_NOT_EXIST, ",错误msg:,", err)
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, data)
}
